local cuipo = fk.CreateSkill{
  name = "hanqing__cuipo",
  tags = { Skill.Limited },
}

Fk:loadTranslationTable{
  ["hanqing__cuipo"] = "焠魄",
  [":hanqing__cuipo"] = "限定技，回合开始时，你可以废除一个区域，然后获得弃牌堆中一张火属性牌并令此后的"..
  "<a href=':hanqing__burst'>“爆发”</a>每段亮出牌数+1。"..
  "你之后的两个回合开始时重复以上效果，废除所有区域后，将你的回合改为令牌堆<a href=':hanqing__burst'>“爆发”</a>。",

  ["#hanqing__cuipo-choice"] = "焠魄：废除一个区域，获得一张火属性牌并令“爆发”亮出牌数+1",
}

local U = require "packages/hanqing/util"

cuipo:addEffect(fk.TurnStart, {
  can_trigger = function (self, event, target, player, data)
    return target == player and
      ((player:hasSkill(cuipo.name) and player:usedSkillTimes(cuipo.name, Player.HistoryGame) == 0) or
      (#player:getTableMark(cuipo.name) > 0 and #player:getTableMark(cuipo.name) < 3))
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local choices = { "$Hand", "$Equip", "$Judge" }
    if player:usedSkillTimes(cuipo.name, Player.HistoryGame) == 0 then
      table.insert(choices, "Cancel")
    else
      choices = table.filter(choices, function (choice)
        return not table.contains(player:getTableMark(cuipo.name), choice)
      end)
    end
    local choice = room:askToChoice(player, {
      choices = choices,
      skill_name = cuipo.name,
      prompt = "#hanqing__cuipo-choice",
    })
    if choice ~= "Cancel" then
      event:setCostData(self, {choice = choice})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local choice = event:getCostData(self).choice
    room:addTableMark(player, cuipo.name, choice)
    if choice == "$Hand" then
      if not player:isKongcheng() then
        room:moveCardTo(player:getCardIds("h"), Card.DiscardPile, player, fk.ReasonPutIntoDiscardPile)
      end
    elseif choice == "$Equip" then
      room:abortPlayerArea(player, player:getAvailableEquipSlots())
    elseif choice == "$Judge" then
      room:abortPlayerArea(player, Player.JudgeSlot)
    end
    if player.dead then return end
    if not table.contains(player:getTableMark(cuipo.name), "$Hand") then
      local cards = table.filter(room.discard_pile, function (id)
        local card = Fk:getCardById(id)
        return card.is_damage_card and table.contains({"fire__slash", "fire_attack", "burning_camps"}, card.name)
      end)
      if #cards > 0 then
        local card = room:askToChooseCard(player, {
          target = player,
          flag = { card_data = {{ "pile_discard", cards }} },
          skill_name = cuipo.name,
        })
        room:moveCardTo(card, Card.PlayerHand, player, fk.ReasonJustMove, cuipo.name, nil, true, player)
        if player.dead then return end
      end
    end
    local info = room:getBanner("hanqing__burst")
    if info then
      local n = info.n or 2
      info.n = n + 1
      room:setBanner("hanqing__burst", info)
    end
    if #player:getTableMark(cuipo.name) == 3 then
      U.burst(player)
      room.logic:breakTurn()
    end
  end,
})

cuipo:addEffect(fk.PreTurnStart, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    return target == player and #player:getTableMark(cuipo.name) == 3 and not data.turn_end
  end,
  on_use = function (self, event, target, player, data)
    data.turn_end = true
    U.burst(player)
  end,
})

cuipo:addEffect(fk.BeforeCardsMove, {
  can_refresh = function(self, event, target, player, data)
    if table.contains(player:getTableMark(cuipo.name), "$Hand") then
      for _, move in ipairs(data) do
        if move.to == player and move.toArea == Card.PlayerHand then
          return true
        end
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local ids = {}
    for _, move in ipairs(data) do
      if move.to == player and move.toArea == Card.PlayerHand then
        for _, info in ipairs(move.moveInfo) do
          table.insert(ids, info.cardId)
        end
      end
    end
    room:cancelMove(data, ids)
  end,
})
return cuipo
